
$(function () {
    var colunms = Menu.initColumn();
    var table = new TreeTable(Menu.id, baseURL + "HelpCategory/helpcategory/list/tree", colunms);
    table.setExpandColumn(2);
    table.setIdField("id");
    table.setCodeField("id");
    table.setParentCodeField("pid");
    table.setExpandAll(false);
    table.init();
    Menu.table = table;
});
var setting = {
	data: {
		simpleData: {
			enable: true,
			idKey: "id",
			pIdKey: "pid",
			rootPId: -1
		},
		key: {
			url:"nourl"
		}
	}
};
var ztree;
var Menu = {
    id: "bootstrap-table",
    table: null,
    layerIndex: -1
};
/**
 * 初始化表格的列
 */
Menu.initColumn = function () {
    var columns = [
        {field: 'selectItem', radio: true},
        {title: '分类ID', field: 'id', visible: false, align: 'center', valign: 'middle', width: '80px'},
        {title: '名称', field: 'name', align: 'center', valign: 'middle', sortable: true, width: '180px'},
        {title: '等级', field: 'level', align: 'center', valign: 'middle', sortable: true, width: '100px'}
        ]
    return columns;
};
var vm = new Vue({
	el:'#rrapp',
	data:{
		search : {
					
				},
		showList: true,
		title: null,
		helpCategory: {}
	},
	methods: {
		getMenu: function(menuId){
			//加载菜单树
			$.get(baseURL + "HelpCategory/helpcategory/list/select", function(r){
				ztree = $.fn.zTree.init($("#menuTree"), setting, r.menuList);
				var node = ztree.getNodeByParam("id", vm.helpCategory.pid);
				ztree.selectNode(node);
				vm.helpCategory.parentName = node.name;
			})
		},
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.helpCategory = {parentName:null,pid:0};
			vm.getMenu();
		},
		update: function (event) {
			var id = getMenuId();
			if(id == null){
				return ;
			}
			vm.showList = false;
            vm.title = "修改";
            vm.getInfo(id);
 		},
		saveOrUpdate: function (event) {
			var url = vm.helpCategory.id == null ? "HelpCategory/helpcategory/save" : "HelpCategory/helpcategory/update";
			$.ajax({
				type: "POST",
			    url: baseURL + url,
                contentType: "application/json",
			    data: JSON.stringify(vm.helpCategory),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		del: function (event) {
			var id = getMenuId();
			if(id == null){
				return ;
			}
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: baseURL + "HelpCategory/helpcategory/delete",
					data: "menuId=" + id,
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								vm.reload();
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		getInfo: function(id){
			$.get(baseURL + "HelpCategory/helpcategory/info/"+id, function(r){
                vm.helpCategory = r.helpCategory;
				vm.getMenu();
            });
		},
		reload: function (event) {
			vm.showList = true;
			Menu.table.refresh();

		},
		menuTree: function(){
			layer.open({
				type: 1,
				offset: '50px',
				skin: 'layui-layer-molv',
				title: "选择分类",
				area: ['300px', '450px'],
				shade: 0,
				shadeClose: false,
				content: jQuery("#menuLayer"),
				btn: ['确定', '取消'],
				btn1: function (index) {
					var node = ztree.getSelectedNodes();
					//选择上级菜单
					vm.helpCategory.pid = node[0].id;
					vm.helpCategory.parentName = node[0].name;
					layer.close(index);
				}
			});
		},
	}
});


		function edit(id) {

			layer.open({
				type : 2,
				title : '编辑',
				maxmin : true,
				//shadeClose : false, // 点击遮罩关闭层
				area : [ '800px', '520px' ],
				content : "helpCategoryEdit.html?id=" + id // iframe的url
			});
		}

		function remove(id) {
			var arr = new Array();
			arr.push(id);
			confirm('确定要删除选中的记录？', function() {

				$.ajax({
					type : "POST",
					url : baseURL + "HelpCategory/helpcategory/delete",
					contentType : "application/json",
					data : JSON.stringify(arr),
					success : function(r) {
						if (r.code == 0) {
							alert('操作成功', function() {
								vm.reload();
							});
						} else {
							alert(r.msg);
						}
					}
				});
			});
		}
function getMenuId () {
	var selected = $('#bootstrap-table').bootstrapTreeTable('getSelections');
	if (selected.length == 0) {
		alert("请选择一条记录");
		return null;
	} else {
		return selected[0].id;
	}
}
